<template>
	<div class="container">
		<div class="sign-in" v-bind:style="{'backgroundImage':'url(agency/assets/images/logo.png)'}">
			<h3>渠道管理分销助手</h3>
			<ul class="sign-list">
				<li>
					<div class="sign-list-row">
						<label class="row-title">帐号</label>
						<div class="row-content">
							<input type="text" placeholder="请输入帐号" v-model="loginData.phone" v-on:blur="getCompanyList">
							<i class="fa fa-times-circle fa-hidden"></i>
							<i class="fa fa-exclamation-circle"></i>
						</div>
					</div>
				</li>
				<li>
					<div class="sign-list-row">
						<label class="row-title">密码</label>
						<div class="row-content">
							<input type="password" placeholder="请输入密码" v-model="loginData.password">
							<i class="fa fa-times-circle "></i>
						</div>
					</div>
				</li>
				<li>
					<div class="sign-list-row">
						<label class="row-title">开发商</label>
						<div class="row-content">
							<select v-model="loginData.companyUuid" @change="setCompanyUuid">
								<option value="">请选择开发商</option>
								<option v-for="company in companyList" v-bind:value="company.companyUuid">{{company.companyName}}</option>
							</select>
						</div>
					</div>
				</li>
                <a href = "javascript:void(0)" @click="toForgetPassword"style="color:#666;float: right;text-decoration: underline;">忘记密码</a>
			</ul>
			<button class="btn btn-sign" @click="loginUser()">登录</button>
			<!--<button class="btn btn-sign"style="margin-top: 15px;" @click="toRegister()">新用户注册</button>-->
		</div>
	</div>
</template>
<script>
	export default{
        data(){
            return {
                logoUrl: '',
                loginData: {
                    phone: '',
                    password: '',
                    wxOpenId: window.sessionStorage.getItem(this.utilHelper.appAgency+"_wxOpenid"),
                    cooCompanyId: '',
					domain: '',
					companyUuid:''
                },
                logoInfo: {
                    logoType: "1"
                },
                companyList: []
            }
        },
        mounted: function () {
            
        }, methods: {
            toForgetPassword(){
                this.$router.push({
                    path:'/forgetPassword'
                });
            },
            getCompanyLogo: function () {
                var _self = this;
                var url = _self.utilHelper.apiUrl + "/api/getLogo";
                var body = {};
                body.params = _self.logoInfo;
                _self.$http.post(url, body).then((response) => {
                    var data = response.body.resData;
                    if (data.result=='1') {
                        _self.logoUrl = data.logoUrl;
                    } else {
                        var pathName = window.document.location.pathname;
                        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
                        _self.logoUrl = projectName + '/src/agency/assets/images/logo.png';
                    }
                })
            },
            loginUser: function () {
                var _self = this;
                if (_self.vaild()) {
                    var body = {};
                    body.params = _self.loginData;
                    var url = _self.utilHelper.apiUrl + "/api/login";
                    _self.$http.post(url, body).then((response) => {
                        var data = response.body.resData;
                        var result = data.result;
                        var errorMsg = data.resultMsg;
                        if (result == "1") {
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_userId", data.id);
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_userName", data.accountName);
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_phone", data.phone);
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_isLogin", true);
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_userRole", data.userRoleId);
                            window.sessionStorage.setItem(_self.utilHelper.appAgency+"_accountType",data.accountType);
                            window.sessionStorage.setItem("view","agencyLogin");
                            window.sessionStorage.setItem("companyUuid",_self.loginData.companyUuid);
                            _self.$router.replace({
                                path: '/agency'
                            })
                        } else {
                            layer.msg(errorMsg, {
                                time: 1000
                            });
                        }
                    }, (response) => {
                        layer.msg('登陆超时', {
                            time: 1000
                        });
                    });
                }
            }, vaild: function () {
                var _self = this;
                if (!_self.loginData.phone) {
                    layer.msg("请输入登录帐号", {
                        time: 2000
                    });
                    return false;
                }
                if (!_self.loginData.password) {
                    layer.msg("请输入登录密码", {
                        time: 2000
                    });
                    return false;
                }
              /*  if (_self.companyList.length == 0) {
                    layer.msg("用户名或密码错误", {
                        time: 2000
                    });
                    return false;
				}*/
                if (!_self.loginData.companyUuid) {
                    layer.msg("请选择开发商", {
                        time: 2000
                    });
                    return false;
				}else{
					//将域名放入缓存中
					window.sessionStorage.setItem(_self.utilHelper.appAgency+"_domain",_self.loginData.companyUuid);
				}
                return true;
            },
            toRegister(){
                this.$router.push({path: '/register'});

            },
            getCompanyList(){
                var _self = this;
                var body = {};
                if (this.loginData.phone) {
                    var params = {
                        phone : this.loginData.phone
                    };
                    body.params = params;
                    var url = _self.utilHelper.apiUrl + "/api/getCompanyByPhone";
                    _self.$http.post(url, body).then((response) => {
                        var data = response.body.resData;
                        var result = data.result;
                        var errorMsg = data.resultMsg;
                        _self.loginData.domain = '';
                        if (result == "1") {
                            _self.companyList = data.companyList;
                            if (data.companyList.length == 1) {
                                _self.loginData.domain = data.companyList[0].domain;
                            }
                        }
                    }, (response) => {

                    });
				}
            },
            setCompanyUuid(){
                var _self = this;
                if(_self.loginData.companyUuid){
                    window.sessionStorage.setItem("companyUuid",_self.loginData.companyUuid);
				}
			}
        }
    }
</script>
<style scoped>
	.row-title{text-align: right;padding-right: 10px;}
	.row-content input{padding-left: 8px;}
	.row-content select{padding-left: 8px;}
	.row-select{height: 24px;line-height: 24px;width: 200px;background-color: #f2f2f2;padding-left: 8px;padding-right: 20px;}
	.row-content .fa-angle-down{position: absolute;right: 8px;top: 2px;display: block;}
	.sign-in1{padding: 130px 20px 50px;background: url({{logoUrl}}) no-repeat center 100px;background-size: 83px auto;height: 100%;}
</style>